#include "../main.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define PATH_FIFO "/home/yujiu/fifo"
int main(){
    int ret=access(PATH_FIFO,F_OK);
    if(ret==-1){
        perror("mkfifo:");
        exit(1);
    }
    int fd=open(PATH_FIFO,O_RDONLY);
    if(fd==-1){
        perror("open:");
        exit(1);
    }
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(fd,&readfds);
    fd_set readfds_bak;
    struct timeval timeout={3,0};
    struct timeval timeout_temp;
    char buffer[128]={0};
    while(1){
        timeout_temp=timeout;
        readfds_bak=readfds;
        ret=select(fd+1,&readfds_bak,NULL,NULL,&timeout_temp);
        if(ret==-1){
            perror("select:");
            exit(1);
        }else if(ret==0){
            printf("超时\n");
        }else{
            if(FD_ISSET(fd,&readfds)){
                memset(buffer,0,sizeof(buffer));
                read(fd,buffer,sizeof(buffer));
                if(strcmp(buffer,"exit")==0){
                    break;
                }
                printf("%s\n",buffer);
            }
        }
    }
    close(fd);
    return 0;
}
